package com.ideal.trsdata.service;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ideal.trsdata.dao.OrgGetDao;
import com.ideal.trsdata.model.SyncLogModel;
@Service
@Transactional
public class OrgGetService {
	private static final Logger logger = Logger.getLogger(OrgGetService.class);
	@Autowired
	private OrgGetDao orgGetDao;
	
	public void getOrgData() {
		Long time1=System.currentTimeMillis();
		logger.info("开始获取政企数据到中间表");
		System.out.println("开始获取政企数据到中间表");
		//初始化同步日志model
		SyncLogModel model= new SyncLogModel();
		model.setStartTime(new Date());
		
		try {
			//获取有效数据
			orgGetDao.insertEffectiveOrgToOut();
			
			//获取无效数据
			orgGetDao.insertInvaildOrgToOut();
		} catch (Exception e) {
			logger.info("获取政企数据到中间表异常");
			logger.error("异常原因:" + e.getMessage());
			e.printStackTrace();
		}

		//记录同步日志
		Long time2=System.currentTimeMillis();
		Long timeAll = time2-time1;
		model.setTimeAll(timeAll);
		insertLog(model);
		System.out.println("获取政企数据到中间表结束");
		logger.info("获取政企数据到中间表结束");
	}
	
	public void insertLog(SyncLogModel model) {
		model.setEndTime(new Date());
		try {
			orgGetDao.insertSyncLog(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}